import string
import urllib.request
from urllib.parse import quote
import pymysql as ps

from bs4 import BeautifulSoup

city = input("请输入城市")

code = {
    '北京': '530',
    '上海': '538',
    '广州': '763',
    '深圳': '765',
    '天津': '531',
    '武汉': '736',
    '西安': '854',
    '成都': '801',
    '沈阳': '599',
    '南京': '635',
    '杭州': '653',
    '苏州': '639',
    '重庆': '551',
    '长沙': '749',
    '厦门': '682',
    '南昌': '691'
}

city = code[city]

type = input("请输入岗位：")

url = "https://sou.zhaopin.com/?jl="+city+"&kw="+type+"&p=1"
# 如果url中需要有空格，这需要手动转译，%20来代替空格
#url
url = quote(url,safe=string.printable)
res = urllib.request.urlopen(url)

print(url)

print(res)

content = res.read().decode("utf8")
# print(content)

soup = BeautifulSoup(content,"lxml")

jobitem = soup.select(".joblist-box__item")

# print(jobitem)

job_list = []

def nospace(word):
    return word.strip().replace(" ","").replace("\n","")

for job in jobitem:
    job_dic = {}
    # get_text()用来获取所有文本内容，但是是以字符串列表方式显示
    job_name = job.select(".jobinfo__name")[0].get_text()
    job_sal = job.select(".jobinfo__salary")[0].get_text()
    job_sal = job_sal.strip().replace(" ","").replace("\n","")
    # print(job_sal)

    if '天' in job_sal:
        job_sal = int(job_sal.split("-")[0])*30
    elif '千' in job_sal:
        job_sal = int(job_sal.split('千')[0])*1000
    elif '万' in job_sal:
        job_sal = float(job_sal.split('万')[0]) * 10000
    else:
        job_sal = 5000

    company_name = job.select(".companyinfo__name")[0].get_text()
    company_name = nospace(company_name)

    job_msg = job.select(".jobinfo__other-info-item")

    job_place = job_msg[0].get_text()
    job_place = nospace(job_msg[0].get_text())[0:2]


    job_exp = job_msg[1].get_text()
    job_exp = nospace(job_exp)


    job_education = job_msg[2].get_text()
    job_education = nospace(job_education)

    job_dic["job_name"] = job_name
    job_dic["job_sal"] = job_sal
    job_dic["company_name"] = company_name
    job_dic["job_place"] = job_place
    job_dic["job_exp"] = job_exp
    job_dic["job_education"] = job_education

    # print(job_dic)
    job_list.append(job_dic)

db = ps.connect(
    host="localhost",
    port=3306,
    user="root",
    database="crawlerdemo",
    password="458092",
    charset="utf8"
)

cursor = db.cursor()

print(job_list)

for job in job_list:
    sql = ("insert into job (job_name,job_sal,company_name,job_place,job_exp,job_education) values "
           "('%s','%s','%s','%s','%s','%s')"%(job["job_name"],job["job_sal"],job["company_name"],job["job_place"],job["job_exp"],job["job_education"]))
    try:
        cursor.execute(sql)
    except Exception as e:
        print(e)
        print("插入数据失败")
db.commit()

cursor.close()
db.close()